package tool;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;
import javax.swing.JTable;

import javax.swing.filechooser.FileFilter;

public class JTableExportListener implements ActionListener{
	
	JTable table;
	String heading;
	String inscribe;
	public JTableExportListener(JTable table,String 标题,String 落款) {
		this.table=table;
		this.heading=标题;
		this.inscribe=落款;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(table==null){
			JOptionPane.showMessageDialog(null, "JTable 为 null，无法导出");
			return;
		}
		if(table.getRowCount()==0){
			JOptionPane.showMessageDialog(null, "表格内没有内容，无法导出");
			return;
		}
		JFileChooser fc=new JFileChooser();
		fc.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				return ".xls";
			}
			
			@Override
			public boolean accept(File f) {
				if(f.getName().endsWith(".xls")){
					return true;
				}else{
					return false;
				}
			}
		});
		int result=fc.showSaveDialog(null);
		if(result==JFileChooser.APPROVE_OPTION){
			File file=fc.getSelectedFile();
			if(!file.getName().endsWith(".xls")){
				file=new File(file.getAbsoluteFile()+".xls");
			}
			JTableToExcel.export(file, heading, inscribe, table);
		}
	}

}
